
#include"comm.h"
#include<unistd.h>

int main()
{
    //ftok创建key (自定义路径，自定义ID)
    key_t key=ftok(PATH_NAME,PROJ_ID);
    if(key<0)
    {
        perror("ftok"); return 1;
    }
    
    //创建共享内存，返回的是共享内存的标识码
     int shmid=shmget(key,SIZE,IPC_CREAT);
    if(shmid<0)
    {
        perror("shmget"); return 2;
    }

    printf("key:%u,shmid:%d\n",key,shmid);

    //attaches the shared memory
    //返回指向共享内存的一个地址的指针
    char* mesg=(char*)shmat(shmid,NULL,0);

    printf("attaches the shared memory success!\n");

    //通信开始
    while(1){
        sleep(1); printf("%s\n",mesg);
    }

    //detaches the shared memory
    shmdt(mesg);
    printf("detaches shm success!\n");

    //删除共享内存段
    shmctl(shmid,IPC_RMID,NULL);
    printf("key:0x%x,shmid:%d->shm delete success\n",key,shmid);
    return 0;

}
